package com.sjj.springcloud.controller;

import com.sjj.springcloud.pojo.Dept;
import com.sjj.springcloud.service.Impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//提供Restful服务!
@RestController
@Slf4j
public class DeptController {

    @Resource
    private DeptServiceImpl deptService;
    @Autowired
    private DiscoveryClient client;


    @RequestMapping(value = "dept/add",produces = "application/json")
    public boolean addDept(Dept dept){
        return deptService.add(dept);
    }
    @GetMapping("dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.get(id);
    }
    @GetMapping("dept/getAll")
    public List<Dept> getAll(){
        return deptService.getAll();
    }
    @GetMapping("dept/discovery")
    public Object discovery(){
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery==>services:"+services);

        //得到一个具体的微服务信息,通过具体的微服务id:spring.application.name
        List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
        for (ServiceInstance instance:instances){
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getInstanceId()
            );
        }
        return this.client;
    }
}
